package com.souche.employment.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.souche.employment.CommonUtils;
import com.souche.employment.domain.JobDO;
import com.souche.employment.dto.JobDto;
import com.souche.employment.interceptor.AuthHolder;
import com.souche.employment.mapper.JobMapper;
import com.souche.employment.response.CustomException;
import com.souche.employment.response.DataResult;
import com.souche.employment.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 
 * BaseController.java created at 2017年8月8日 下午1:11:20
 * @author Ray [email:rayinhangzhou@hotmail.com]
 * Revision History
 * Date            Programmer                   Notes
 * ----------------------------------------------------------
 **/

@RestController
@RequestMapping(value = "/job")
@Api(value = "/job", description = "职位接口")
public class JobController {
	@Autowired
	private JobMapper jobMapper;
    @Autowired
    private CommonService commonService;


    @RequestMapping(value="/getForList", method=RequestMethod.GET)
    @ApiOperation(value="获取职位筛选列表")
    public DataResult getForList() {
        List<JobDO> list = jobMapper.getForList();
        List<JobDto> result = new ArrayList<>();
        if (list!=null) {
            list.stream().forEach(JobDO -> result.add(convertDo2Dto(JobDO)));
        }
        return DataResult.success(result);
    }

	@RequestMapping(value="/search", method=RequestMethod.GET)
	@ApiOperation(value="搜索职位")
	public DataResult search(
            @ApiParam(value = "名称") @RequestParam(required = false) String name,
            @ApiParam(value = "一级部门") @RequestParam(required = false) String dep1,
            @ApiParam(value = "二级部门") @RequestParam(required = false) String dep2,
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String sort) {
        sort = StringUtils.isBlank(sort)?"date_update desc":sort;
        PageHelper.startPage(pageNum, pageSize);
        List<JobDO> list = jobMapper.search(name, dep1, dep2, sort);
        List<JobDto> result = new ArrayList<>();
        if (list!=null) {
            list.stream().forEach(item -> result.add(convertDo2Dto(item)));
        }
        PageInfo<JobDto> pageInfo = new PageInfo<>(result);
        return DataResult.success(pageInfo);
	}

	@RequestMapping(value = "/addOrEdit", method = RequestMethod.POST)
	@ApiOperation(value = "添加编辑职位")
	public DataResult addOrEdit(
			@RequestParam(required = false) String id,
			@ApiParam(value = "名称", required = true) @RequestParam String name,
			@ApiParam(value = "一级部门", required = true) @RequestParam String dep1,
			@ApiParam(value = "二级部门", required = true) @RequestParam String dep2,
			@ApiParam(value = "网站链接") @RequestParam(required = false) String jd) {
		if (StringUtils.isBlank(id)) {
			JobDO jobDO = new JobDO();
			jobDO.setId(CommonUtils.getShortUuid());
			jobDO.setName(name);
			jobDO.setDep1(dep1);
			jobDO.setDep2(dep2);
            jobDO.setJd(jd);
            jobDO.setCreator(AuthHolder.mail());
            jobDO.setDateCreate(Calendar.getInstance().getTime());
            jobDO.setDateUpdate(Calendar.getInstance().getTime());
            jobMapper.addJob(jobDO);
            return DataResult.success(jobDO);
        } else {
            JobDO jobDO = jobMapper.getJob(id);
            if (jobDO==null) {
                throw CustomException.fail("该职位不存在！", null);
            } else {
                jobDO.setName(name);
                jobDO.setDep1(dep1);
                jobDO.setDep2(dep2);
                jobDO.setJd(jd);
                jobDO.setCreator(AuthHolder.mail());
                jobMapper.updateJob(jobDO);
                return DataResult.success(jobDO);
            }
        }
	}

    @RequestMapping(value="/delete", method=RequestMethod.POST)
    @ApiOperation(value="删除职位")
    public DataResult delete(
            @RequestParam String id) {
        jobMapper.deleteJob(id);
        return DataResult.success(id);
    }
	


    private JobDto convertDo2Dto(JobDO jobDO) {
        JobDto dto = new JobDto(jobDO);
        dto.setDep1Name(commonService.getDepNameById(jobDO.getDep1()));
        dto.setDep2Name(commonService.getDepNameById(jobDO.getDep2()));
        return dto;
    }
}
